package com.admin.system.modules.system.rest;

import com.admin.common.utils.PageResult;
import com.admin.component.web.MultipartFileUtil;
import com.admin.core.dto.ParamConfigQueryDTO;
import com.admin.core.entity.SysParamConfig;
import com.admin.logging.annotation.Log;
import com.admin.logging.enums.OperateTypeEnum;
import com.admin.system.modules.system.service.ParamConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sq-lan
 * @date 2024-05-11
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "参数配置管理")
@RequestMapping("/api/paramConfig")
public class ParamConfigController {

    private final ParamConfigService paramConfigService;

    @GetMapping
    @ApiOperation("分页查询参数配置")
    @PreAuthorize("@ss.check('paramConfig:list')")
    public ResponseEntity<PageResult<SysParamConfig>> paging(ParamConfigQueryDTO dto) {
        return new ResponseEntity<>(paramConfigService.paging(dto), HttpStatus.OK);
    }

    @PostMapping
    @Log(module = "参数配置管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation("新增参数配置")
    @PreAuthorize("@ss.check('paramConfig:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody SysParamConfig entity) {
        paramConfigService.save(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log(module = "参数配置管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation("修改参数配置")
    @PreAuthorize("@ss.check('paramConfig:edit')")
    public ResponseEntity<Object> modify(@Validated @RequestBody SysParamConfig entity) {
        paramConfigService.updateById(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log(module = "参数配置管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation("删除参数配置")
    @PreAuthorize("@ss.check('paramConfig:del')")
    public ResponseEntity<Object> remove(@RequestBody List<Integer> ids) {
        paramConfigService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('paramConfig:list')")
    public void export(HttpServletResponse response, ParamConfigQueryDTO dto) throws IOException {
        List<SysParamConfig> list = paramConfigService.list(dto);
        List<Map<String, Object>> result = new ArrayList<>();
        for (SysParamConfig paramConfig : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("参数名称", paramConfig.getName());
            map.put("参数键名", paramConfig.getCode());
            map.put("参数键值", paramConfig.getValue());
            map.put("系统内置（Y是 N否）", paramConfig.getType());
            map.put("创建者", paramConfig.getCreateBy());
            map.put("创建时间", paramConfig.getCreateTime());
            map.put("更新者", paramConfig.getUpdateBy());
            map.put("更新时间", paramConfig.getUpdateTime());
            map.put("备注", paramConfig.getRemark());
            result.add(map);
        }
        MultipartFileUtil.downloadExcel(result, response);
    }

}
